package com.example.finalwork;

import android.content.Context;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.example.finalwork.DAO.IncomeDAO;
import com.example.finalwork.lmpl.IncomeDAOlmpl;
import com.example.finalwork.DAO.PayDAO;
import com.example.finalwork.lmpl.PayDAOlmpl;
import com.example.finalwork.DAO.UserDAO;
import com.example.finalwork.lmpl.UserDAOlmpl;
import com.example.finalwork.Entity.Income;
import com.example.finalwork.Entity.Pay;
import com.example.finalwork.Entity.User;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    private static final String TAG = "测试:";

    @Test
    public void TestAddUser(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        UserDAO userDAO = new UserDAOlmpl(appContext);
        User user = new User();
        user.setUserName("1234");
        user.setPassWord("1234");
        long result = userDAO.addUser(user);
        Log.d(TAG,"add user result - >" + result);
        assertNotEquals(-1,result);
    }
    @Test
    public void TestGetAllUser(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        UserDAO userDAO = new UserDAOlmpl(appContext);
        List<User> users = userDAO.GetAllUser();
        for(int i = 0 ; i < users.size() ; i++)
        {
            User olduser = users.get(i);
        }
    }
    @Test
    public void TestAddIncome(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        IncomeDAO incomeDAO = new IncomeDAOlmpl(appContext);
        Income income = new Income();
        income.setIn_id(1);
        income.setIn_User("djq");
        income.setIn_Name("123123123");
        income.setIn_Date("2012/02/02");
        income.setIn_Amount("123");
        income.setIn_Remark("123");
        long result = incomeDAO.addIncome(income);
        Log.d(TAG,"add Income result - >" + result);
        assertNotEquals(-1,result);
    }
    @Test
    public void TestupdateIncome(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        IncomeDAO incomeDAO = new IncomeDAOlmpl(appContext);
        Income income = new Income();
        income.setIn_id(1);
        income.setIn_User("yqc");
        income.setIn_Name("1");
        income.setIn_Date("2012/02/02");
        income.setIn_Amount("123");
        income.setIn_Remark("123");
        int result = incomeDAO.updateIncome(income);
        Log.d(TAG,"update user result - >" + result);
        assertNotEquals(-1,result);
    }
    @Test
    public void TestdelIncome(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        IncomeDAO incomeDAO = new IncomeDAOlmpl(appContext);
        int result = incomeDAO.delIncomeById(1);
        Log.d(TAG,"del user result - >" + result);
        assertNotEquals(-1,result);
    }
    @Test
    public void TestGetAllIncome(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        IncomeDAO incomeDAO = new IncomeDAOlmpl(appContext);
        List<Income> incomes = incomeDAO.GetAllIncome();
        for(int i = 0 ; i < incomes.size() ; i++)
        {
            Income income = incomes.get(i);
            Log.d(TAG,income.getIn_User());
        }
    }

    @Test
    public void TestAddPay(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        PayDAO payDAO = new PayDAOlmpl(appContext);
        Pay pay = new Pay();
        pay.setPay_Id(2);
        pay.setPay_User("yqc");
        pay.setPay_Name("123123123");
        pay.setPay_Date("2012/02/02");
        pay.setPay_Amount("123");
        pay.setPay_Remark("123");
        long result = payDAO.addPay(pay);
        Log.d(TAG,"add pay result - >" + result);
        assertNotEquals(-1,result);
    }
    @Test
    public void TestupdatePay(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        PayDAO payDAO = new PayDAOlmpl(appContext);
        Pay pay = new Pay();
        pay.setPay_Id(2);
        pay.setPay_User("djq");
        pay.setPay_Name("123123123");
        pay.setPay_Date("2012/02/02");
        pay.setPay_Amount("123");
        pay.setPay_Remark("123");
        int result = payDAO.updatePay(pay);
        Log.d(TAG,"update user result - >" + result);
        assertNotEquals(-1,result);
    }
    @Test
    public void TestdelPay(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        PayDAO payDAO = new PayDAOlmpl(appContext);
        int result = payDAO.delPayById(2);
        Log.d(TAG,"del pay result - >" + result);
        assertNotEquals(-1,result);
    }
    @Test
    public void TestGetAllPay(){
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        PayDAO payDAO = new PayDAOlmpl(appContext);
        List<Pay> pays = payDAO.GetAllPay();
        for(int i = 0 ; i < pays.size() ; i++)
        {
            Pay pay = pays.get(i);
            Log.d(TAG,pay.toString());
        }
    }



    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.example.finalwork", appContext.getPackageName());
    }
}